//
//  ST_MyFollowLoveViewController.swift
//  btvideo
//
//  Created by mac on 2020/9/14.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import MJRefresh

class ST_MyFollowLoveViewController: UIViewController {
    
    func fetch_boot_data() {
        currentPage = 0
        request_data()
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white

         
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {
            self.refresh_data()
        })
        
        fetch_boot_data()
    }
    
    fileprivate var currentPage = 0
    
    fileprivate var data_array = [ST_BTSweetheart]()
    
    fileprivate func request_data(){
        
        JVrequest(PH.myLoveListApi, success: { (json) in
            
            let array = json["data"]["like_list"].arrayValue.map { (itemJson) -> ST_BTSweetheart in
                return ST_BTSweetheart.init(fromJson: itemJson)
            }
            
            
            if self.currentPage == 1{
                self.data_array = array
            }else{
                self.data_array += array
            }
            
            self.collectionView.reloadData()
            
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
            
        }) { (code, err) in
            
            self.toast(text: err)
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
        }
        
        
    }
    
    @objc func clear_data(){
        currentPage = 0
        self.data_array.removeAll()
        self.collectionView.reloadData()
        
        self.hidden_indeterminate_h_u_d()
        
    }
    
    @objc func refresh_data(){
        
        currentPage = 0
        
        request_data()
        
    }
    
    //     MARK: - Properties


   fileprivate lazy var layout:UICollectionViewFlowLayout = {
      let controlLayout = UICollectionViewFlowLayout()
       controlLayout.minimumLineSpacing = 15
       controlLayout.minimumInteritemSpacing = 15
       controlLayout.sectionInset = UIEdgeInsets.init(top: 12, left: 15, bottom: BTSafeAreaBottom + 49, right: 15)
        let w = (ScreenWidth - 15*3)/2
        controlLayout.itemSize = CGSize.init(width: w, height: w/165*160+70)
       return controlLayout
   }()
   
   fileprivate lazy var collectionView:UICollectionView = {
       let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
       control.delegate = self
       control.dataSource = self
       control.register(cellWithClass: ST_MyLoveCell.self)
    
       if #available(iOS 11.0, *) {
           control.contentInsetAdjustmentBehavior = .never
       }
       control.backgroundColor = UIColor.white
//    init(hexString: "#F5F8FA")
       control.alwaysBounceVertical = true
       control.showsVerticalScrollIndicator = false
       control.showsHorizontalScrollIndicator = false
    
    control.emptyDataSetSource = self
    
    control.emptyDataSetDelegate = self
    
    
       return control
   }()
    
    fileprivate var first = true
    
    fileprivate var empty_image_name = "我的-粉丝-空状态"
}


extension ST_MyFollowLoveViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        
        if first{
            self.first = false
            return false
        }else{
            return true
        }
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let attrString = NSMutableAttributedString(string: "喜欢了这里才有值哦")
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        return attrString
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        
        return UIImage()
    }
    
}

extension ST_MyFollowLoveViewController:UICollectionViewDataSource,UICollectionViewDelegate{
    
    
    // MARK: UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return data_array.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_MyLoveCell.self, for: indexPath)
        
        cell.model = data_array[safe:indexPath.item]
        
        return cell
    }

    // MARK: UICollectionViewDelegate
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let m = data_array[safe:indexPath.item] else{
            return
        }

        let vc = ST_PairsCenterViewController()
        
        vc.accid = m.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
    
}

